<template>
  <div class="quizzDetail">
    <header>
      <div class="header-container">
        <p class="title">{{paperTitle}}</p>
        <p>我的分数：{{totalScore}}</p>
      </div>
    </header>
    <div class="container">
      <aside>
        <ul>
          <li
            v-for="(q, index) of ques"
            :key="q.id"
            :class="{ selected: q.id === que.id }"
            @click="change(q, index)">
            {{ index + 1}}.<i :class="{
              correct: q.ans === q.userAns,
              wrong: q.ans !== q.userAns,
            }"></i>
          </li>
        </ul>
      </aside>
      <main>
        <p class="desc">{{ que.queTypeName }} 问题 {{ now }} / {{ total }}</p>
        <div class="que-container">
          <p class="title">{{ que.name }}</p>
          <div class="ans-container" v-if="que.queType !== 2">
            <div :class="{ ans: true, yes: que.ans.includes('A') }">A. {{ que.options.A }}</div>
            <div :class="{ ans: true, yes: que.ans.includes('B') }">B. {{ que.options.B }}</div>
            <div :class="{ ans: true, yes: que.ans.includes('C') }">C. {{ que.options.C }}</div>
            <div :class="{ ans: true, yes: que.ans.includes('D') }">D. {{ que.options.D }}</div>
          </div>
          <p class="res"><i :class="{
            correct: que.ans === que.userAns,
            wrong: que.ans !== que.userAns,
          }"></i>你的选择：{{ que.userAns }} 正确答案：{{ que.ans }}</p>
        </div>
        <div class="comment">
          <header>
            <p class="count">{{ commentNum }}条评论</p>
          </header>
          <main>
            <div class="input">
              <img width="48" height="48" src="@/assets/test/thumb1.jpg">
              <textarea v-model="com" placeholder="说点什么吧"></textarea>
            </div>
            <div style="text-align:right;margin:10px 0">
              <div class="btn primary" @click="addComment">回复</div>
            </div>
            <div v-for="c of comments" :key="c.id">
              <div class="row">
                <img width="48" height="48" :src="c.headpic">
                <div class="text-content">
                  <p class="name">{{c.user}}<span class="desc">发布于{{c.createDate}}</span></p>
                  {{c.desc}}
                </div>
              </div>
            </div>
          </main>
        </div>
      </main>
    </div>
  </div>
</template>
<script>
import api from '@/api/quizz';
import nginx from '@/assets/nginxconfig';

export default {
  name: 'QuizzDetail',
  data() {
    return {
      paperTitle: '',
      totalScore: '',
      now: 1,
      total: 5,
      que: null,
      ques: [],
      com: '',
      comments: [],
      answers: [],
      commentNum: 0,
    };
  },
  methods: {
    change(que, index) {
      this.now = index + 1;
      this.que = { ...que };
      this.getComment();
    },
    getPaper() {
      api.detail(this.$route.query.paperId).then(async (res) => {
        this.paperTitle = res.data.data.bexam ? res.data.data.bexam.title : '自测试卷';
        this.totalScore = res.data.data.score;
        const datas = res.data.data.bquestionList;
        this.ques = [];
        for (let i = 0; i < datas.length; i += 1) {
          if (datas[i].type === '2') {
            datas[i].answer = datas[i].answer === '0' ? '正确' : '错误';
            // eslint-disable-next-line no-nested-ternary
            datas[i].chosenAnswer = datas[i].chosenAnswer ? datas[i].chosenAnswer === '0' ? '正确' : '错误' : '未选择';
          }
          this.ques.push({
            id: datas[i].businessId,
            name: datas[i].title,
            options: {
              A: datas[i].boptionList ? datas[i].boptionList[0].optionContent : '',
              B: datas[i].boptionList ? datas[i].boptionList[1].optionContent : '',
              C: datas[i].boptionList ? datas[i].boptionList[2].optionContent : '',
              D: datas[i].boptionList ? datas[i].boptionList[3].optionContent : '',
            },
            // eslint-disable-next-line no-nested-ternary
            queType: datas[i].type === '0' ? 0 : datas[i].type === '1' ? 1 : 2,
            // eslint-disable-next-line no-nested-ternary
            queTypeName: datas[i].type === '0' ? '单选题' : datas[i].type === '1' ? '多选题' : '判断题',
            ans: datas[i].answer,
            userAns: datas[i].chosenAnswer ? datas[i].chosenAnswer : '',
          });
        }
        this.total = this.ques.length;
        this.now = 1;
        if (this.ques.length !== 0) {
          this.que = this.ques[0];
          this.getComment();
        }
      });
    },
    getComment() {
      const params = new URLSearchParams();
      params.append('questionId', this.que.id);
      api.getComment(params).then(async (response) => {
        const datas = response.data.data;
        this.commentNum = datas.length;
        this.comments = [];
        for (let i = 0; i < datas.length; i += 1) {
          this.comments.push({
            id: datas[i].businessId,
            user: datas[i].username,
            desc: datas[i].description,
            createDate: datas[i].createDate.split(' ')[0],
            headpic: nginx.nginxaddress + datas[i].headpic,
          });
        }
      });
    },
    addComment() {
      const params = new URLSearchParams();
      params.append('questionId', this.que.id);
      params.append('userId', sessionStorage.getItem('userId'));
      params.append('description', this.com);
      api.addByUserId(params).then(async (response) => {
        if (response.data.code === 200) {
          this.$alert('添加成功');
          this.com = '';
          this.getComment();
        } else {
          this.$alert(response.data.message);
        }
      });
    },
  },
  mounted() {
    this.getPaper();
  },
  created() {
    this.que = { ...this.ques[0] };
  },
};
</script>
<style lang="less">
@import url(../../../../static/style/site.vars.less);
.quizzDetail {
  &>header {
    padding: 30px 0;
    padding-bottom: 50px;
    background: rgba(58, 144, 243, 0.308);
    .header-container {
      // width: 1200px;
      margin: 0 auto;
      padding-left: 30px;
      .title {
        font-size: 24px;
        margin-bottom: 10px;
      }
    }
  }
  &>.container {
    display: flex;
    margin: 0 auto;
    height: calc(100vh - 195px);
    &>aside {
      width: 200px;
      // height: 300px;
      background: #FAFAFA;
      border-right: 1px solid #ededef;
      ul {
        list-style: none;
        li {
          padding-left: 40px;
          padding-top: 15px;
          border-bottom: 1px solid #ededef;
          display: flex;
          cursor: pointer;
          &:hover {
            background: #ECF1FA;
          }
          &.selected {
            background: #ECF1FA;
          }
        }
      }
    }
    &>main {
      width: 100%;
      background: #fff;
      // height: 300px;
      padding: 40px;
      box-sizing: border-box;
      overflow-y: auto;
      .que-container {
        .title {
          font-size: 24px;
          margin-top: 10px;
          margin-bottom: 10px;
        }
        .ans-container {
          .ans {
            margin-bottom: 5px;
            font-size: 18px;
            &.yes {
              color: rgb(0, 236, 0);
            }
            &.no {
              color: red;
            }
          }
        }
        .res {
          position: relative;
          left: -10px;
          display: flex;
          margin-top: 30px;
          align-items: center;
          .wrong, .correct {
            margin-left: 0;
            position: relative;
            top: 2px;
          }
        }
      }
      .comment {
        margin-bottom: 80px;
        &>header {
          padding: 20px 0;
          border-bottom: 1px solid rgba(190, 190, 190, 0.356);
        }
        &>main {
          .input {
            margin-top: 10px;
            text-align: right;
            display: flex;
            &>img {
              border-radius: 46px;
              margin-right: 20px;
            }
            &>textarea {
              width: 100%;
              height: 88px;
            }
          }
          .row {
            margin: 10px 0;
            display: flex;
            &>img {
              border-radius: 46px;
              margin-right: 20px;
            }
            .text-content {
              background: rgb(243, 243, 243);
              width: 100%;
              padding: 20px;
              .name {
                color: @site-main;
                margin-bottom: 10px;
                .desc {
                  margin-left: 10px;
                }
              }
            }
          }
        }
      }
    }
  }
  .desc {
    color: rgb(153, 153, 153);
    font-size: 12px;
  }
  .correct, .wrong{
    display: inline-block;
    height: 32px;
    width: 32px;
    margin-left: 20px;
    position: relative;
    top: -6px;
    background: url(../../../assets/img/icon.png);
  }
  .correct {
    background-position: 32px 0;
  }
  .wrong {
    background-position: 32px 32px;
  }
}
</style>
